package drds.binlog.event_filter.aviater;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import drds.binlog.common.Authors;
import drds.binlog.event_filter.EventFilter;
import drds.common.$;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

/**
 * 基于aviater进行tableName简单过滤计算，不支持正则匹配
 */
@Author(name = Authors.LI_YANG)
public class SimpleFilter implements EventFilter<String>
{

    //include(list,target)
    @Setter
    @Getter
    private final Expression expression = AviatorEvaluator.compile("include(list,target)", true);
    @Setter
    @Getter
    private final List<String> list;

    /**
     * @param expression ,进行分割
     */
    public SimpleFilter(String expression)
    {
        if ($.isNullOrEmpty(expression))
        {
            list = new ArrayList<String>();
        } else
        {
            list = Arrays.asList(expression.split(","));
        }
    }

    public boolean match(String string)
    {
        if (list.isEmpty())
        {
            return true;
        }
        if ($.isNullOrEmpty(string))
        {
            return true;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("target", string);
        return (Boolean) expression.execute(map);
    }

}
